package cn.jcet.module.sys.controller;


import cn.jcet.common.utils.DataGridView;
import cn.jcet.common.utils.ResultObj;
import cn.jcet.common.utils.TreeNode;
import cn.jcet.module.sys.domain.Dept;
import cn.jcet.module.sys.service.IDeptService;
import cn.jcet.module.sys.vo.DeptVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 鲜磊
 * @since 2019-12-17
 */
@RestController
@RequestMapping("dept")
public class DeptController {


    @Autowired
    private IDeptService deptService;

    /**
     * 加载左边树的部门管理树
     * @param deptVo
     * @return
     * loadMenuManagerLeftTreeJson
     */
    @PostMapping("loadDeptManagerLeftTreeJson")
    public DataGridView loadDeptManagertLeftTreeJson(DeptVo deptVo){
        /**
         * 查询全部部门
         */
        List<Dept> list = this.deptService.list();

        List<TreeNode> treeNodes = new ArrayList<>();

        for (Dept dept:list){
           Boolean spread = dept.getOpen()==1?true:false;

           treeNodes.add(new TreeNode(dept.getId(),dept.getPid(),dept.getTitle(),spread));
        }
        return new DataGridView(treeNodes);
    }


    /**
     * 查询全部部门
     * @param deptVo
     * @return
     */
    @GetMapping("loadAllDept")
    public DataGridView loadAllDept(DeptVo deptVo){
        IPage<Dept> page = new Page<>(deptVo.getPage(),deptVo.getLimit());

        QueryWrapper<Dept> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(deptVo.getTitle()),"title",deptVo.getTitle());
        queryWrapper.like(StringUtils.isNotBlank(deptVo.getAddress()),"address",deptVo.getAddress());
        queryWrapper.like(StringUtils.isNotBlank(deptVo.getRemark()),"remark",deptVo.getRemark());
        queryWrapper.eq(deptVo.getId()!=null,"id",deptVo.getId())
                .or().eq(deptVo.getId()!=null,"pid",deptVo.getId());
        queryWrapper.orderByAsc("ordernum");
        this.deptService.page(page,queryWrapper);
        return new DataGridView(page.getTotal(),page.getRecords());
    }

    /**
     * 初始化排序
     * @return
     */
    @GetMapping("loadDeptMaxOrderNum")
    public Map<String,Object> loadDeptMaxOrderNum(){
        Map<String,Object> map = new HashMap<>();

        QueryWrapper<Dept> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("ordernum");
        List<Dept> list = this.deptService.list(queryWrapper);
        if(list.size() >0){
            map.put("value",list.get(0).getOrdernum()+1);
        }else {
            map.put("value",1);
        }
        return map;
    }

    //添加
    @PostMapping("addDept")
    public ResultObj addDept(DeptVo deptVo){
        try {
            deptVo.setCreatetime(new Date());
            this.deptService.save(deptVo);
            return ResultObj.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 修改
     * @param deptVo
     * @return
     */
    @PostMapping("updateDept")
    public ResultObj updateDept(DeptVo deptVo){
        try {
            this.deptService.updateById(deptVo);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    /**
     * 查询当前部门的ID有没有子部门存在
     * @param deptVo
     * @return
     */
    @PostMapping("checkDeptHasChildrenNode")
    public Map<String,Object> checkDeptHasChildrenNode(DeptVo deptVo){
        Map<String,Object> map = new HashMap<>();

        /**
         * 查询子节点
         */
        QueryWrapper<Dept> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid",deptVo.getId());
        List<Dept> list = this.deptService.list(queryWrapper);
        if(list.size()>0){
            map.put("value",true);
        }else {
            map.put("vlaue",false);
        }
        return map;
    }

    /**
     * 删除
     * @param deptVo
     * @return
     */
    @PostMapping("deleteDept")
    public ResultObj deleteDept(DeptVo deptVo){
        try {
            this.deptService.removeById(deptVo.getId());
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }
}
